<template>
  <div id="app">
    <div class="register-container">
      <div class="register-box">
        <div class="register-write">
          <el-form :model="registerForm" :rules="registerRules" ref="registerForm" label-width="0px" class="register-form">
            <el-form-item prop="username" class="form-item">
              <el-input v-model="registerForm.username" placeholder="用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password" class="form-item">
              <el-input type="password" v-model="registerForm.password" placeholder="密码"></el-input>
            </el-form-item>
            <el-form-item prop="phone" class="form-item">
              <el-input v-model="registerForm.phone" placeholder="电话号码(选填)"></el-input>
            </el-form-item>
          </el-form>
        </div>
        <el-button type="primary" @click="handleRegister" :loading="isSubmitting">注册</el-button>
        <el-link type="primary" @click="$router.push({ name: 'login' })">已有账号？登录</el-link>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios';
import { ElMessage } from 'element-plus';

export default {
  data() {
    return {
      registerForm: {
        username: '',
        password: '',
        phone: '',
        role: 'user', // 默认角色
        lastLoginTime: null
      },
      registerRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      isSubmitting: false
    };
  },
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate((valid) => {
        if (valid) {
          this.isSubmitting = true;
          axios.post('http://localhost:8081/api/auth/register', {
            username: this.registerForm.username,
            password: this.registerForm.password,
            phone: this.registerForm.phone,
            role: this.registerForm.role,
            lastLoginTime: this.registerForm.lastLoginTime
          })
              .then(response => {
                console.log('注册成功:', response.data);
                ElMessage({
                  message: '注册成功',
                  type: 'success',
                  duration: 2000
                });
                this.$router.push({name: 'login'});
              })
              .catch(error => {
                console.error('注册失败:', error);
                ElMessage.error('注册失败，请重试');
              })
              .finally(() => {
                this.isSubmitting = false;
              });
        } else {
          console.log('错误: 验证失败。');
          return false;
        }
      });
    }
  }
};
</script>

<style scoped>
.register-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #76ecf8;
}

.register-box {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  width: 400px;
  height: 400px;
  padding: 20px;
  background-color: #ffffff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  border-radius: 8px;
}

.register-write {
  width: 100%;
  height: 200px;
}

.form-item {
  margin-bottom: 20px;
}

.el-button {
  width: 100%;
}
</style>
